package com.hsy.crm.web.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hsy.crm.web.domain.cm.App;
import com.hsy.crm.web.domain.cm.PageResult;
import com.hsy.crm.web.domain.po.report.*;
import com.hsy.crm.web.domain.po.store.StoreReportDetailPo;
import com.hsy.crm.web.domain.po.store.StoreReportSumPo;
import com.hsy.crm.web.domain.vo.report.*;
import com.hsy.crm.web.domain.vo.store.StoreReportDetailVo;
import com.hsy.crm.web.mapper.ReportMapper;
import com.hsy.crm.web.utils.DictUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ReportService {

    @Autowired
    ReportMapper reportMapper;

    public PageResult<PurchaseTrackPo> purchaseTrack(PurchaseTrackVo pageVo) {
        return new PageResult<>(reportMapper.purchaseTrack(pageVo));
    }

    public PageResult<PurchaseDetailPo> purchaseDetail(PurchaseDetailVo pageVo) {
        return new PageResult<>(reportMapper.purchaseDetail(pageVo));
    }

    public PageResult<PurchaseSumPo> purchaseSum(PurchaseSumVo pageVo) {
        PageResult<PurchaseSumPo> page = new PageResult<>();
        if( pageVo.getSumType() == 1 ){
            page.setRows(reportMapper.purchaseSumProduct(pageVo));
        }else if( pageVo.getSumType() == 2 ) {
            page.setRows(reportMapper.purchaseSumVendor(pageVo));
        }
        return page;
    }


    public PageResult<MarketTrackPo> marketTrack(MarketTrackVo pageVo){
        return new PageResult<>(reportMapper.marketTrack(pageVo));
    }

    public PageResult<MarketDetailPo> marketDetail(MarketDetailVo pageVo) {
        return new PageResult<>(reportMapper.marketDetail(pageVo));
    }

    public PageResult<MarketSumPo> marketSum(MarketSumVo pageVo) {
        PageResult<MarketSumPo> page = new PageResult<>();
        if( pageVo.getSumType() == 1 ){
            page.setRows(reportMapper.marketSumProduct(pageVo));
        }else if( pageVo.getSumType() == 2 ){
            page.setRows(reportMapper.marketSumVendor(pageVo));
        }
        return page;
    }

    public PageResult<MarketProfitPo> marketProfit(MarketProfitVo pageVo) {
        if( DictUtils.getDictValue(pageVo.getCustomerId(), App.Dict.VALUATION_TYPE) == App.Dict.ENABLE ){
            return new PageResult<>(reportMapper.marketProfitFifo(pageVo));
        }else{
            return new PageResult<>(reportMapper.marketProfit(pageVo));
        }
    }

    public PageResult<MarketProfitPo> marketOrder(MarketProfitVo pageVo) {
        if(DictUtils.getDictValue(pageVo.getCustomerId(), App.Dict.VALUATION_TYPE) == App.Dict.ENABLE){
            return new PageResult<>(reportMapper.marketProfitFifo(pageVo));
        }else{
            return new PageResult<>(reportMapper.marketProfit(pageVo));
        }
    }

    public PageResult<CustomerCompPo> customerComp(CustomerCompVo pageVo) {
        return new PageResult<>(reportMapper.customerComp(pageVo));
    }

    public PageResult<StoreReportDetailPo> storeDetail(StoreReportDetailVo pageVo) {
        Page page = PageHelper.startPage(pageVo.getPage(),pageVo.getLimit());
        PageResult<StoreReportDetailPo> pageResult = new PageResult<>();
        pageResult.setRows(reportMapper.storeDetail(pageVo));
        pageResult.setTotal(page.getTotal());
        return pageResult;
    }

    public PageResult<StoreReportSumPo> storeSum(StoreReportDetailVo pageVo) {
        Page page = PageHelper.startPage(pageVo.getPage(),pageVo.getLimit());
        PageResult<StoreReportSumPo> pageResult = new PageResult<>();
        pageResult.setRows(reportMapper.storeSum(pageVo));
        pageResult.setTotal(page.getTotal());
        return pageResult;
    }



    public PageResult<VendorCompPo> vendorComp(VendorCompVo pageVo) {
        return new PageResult<>(reportMapper.vendorComp(pageVo));
    }

    public PageResult<ProfitPo> profit(ProfitVo pageVo) {
        return new PageResult<>(reportMapper.profit(pageVo));
    }

}
